package com.axzhengxin.platform.assistant.controller;

import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import com.axzhengxin.platform.assistant.domain.BizMarketUser;
import com.axzhengxin.platform.assistant.service.IBizMarketUserService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysPermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author axzhengxin
 * @date 2021-03-08
 */
@RestController
@RequestMapping("/assistant/user")
public class BizMarketUserController extends BaseController {
    @Autowired
    private IBizMarketUserService bizMarketUserService;
    @Autowired
    private SysPermissionService permissionService;

    /**
     * 营销名单分发以及批量分发的接口
     */
    @PreAuthorize("@ss.hasPermi('assistant:user:distribute')")
    @PostMapping("/distribute")
    public AjaxResult distribute(String[] reqIds, String[] userIds) {
        if (reqIds.length > 0 && userIds.length > 0) {
            AtomicInteger successNum = new AtomicInteger(1);
            Set<String> roles = permissionService.getRolePermission(SecurityUtils.getLoginUser().getUser());
            roles.parallelStream().forEach(role -> {
                if ("ecsc_sub_3_normal".equals(role)) {
                    successNum.set(-1);
                }
            });
            if (successNum.get() > 0) {
                return toAjax(bizMarketUserService.batchSave(reqIds, userIds));
            } else {
                return AjaxResult.error("此用户无权限操作此操作！");
            }
        } else {
            return AjaxResult.error("传值不能为空");
        }
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('customer:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizMarketUser bizMarketUser) {
        startPage();
        List<BizMarketUser> list = bizMarketUserService.selectBizMarketUserList(bizMarketUser);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('customer:user:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizMarketUser bizMarketUser) {
        List<BizMarketUser> list = bizMarketUserService.selectBizMarketUserList(bizMarketUser);
        ExcelUtil<BizMarketUser> util = new ExcelUtil<BizMarketUser>(BizMarketUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bizMarketUserService.selectBizMarketUserById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('customer:user:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizMarketUser bizMarketUser) {
        return toAjax(bizMarketUserService.insertBizMarketUser(bizMarketUser));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('customer:user:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizMarketUser bizMarketUser) {
        return toAjax(bizMarketUserService.updateBizMarketUser(bizMarketUser));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('customer:user:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizMarketUserService.deleteBizMarketUserByIds(ids));
    }
}
